<?php
namespace app\common\repositories\users;

use app\common\repositories\BaseRepository;
use app\common\dao\users\UsersDao;
use think\facade\Db;

class UsersRepository extends BaseRepository
{
    protected $dao;
    public function __construct(){
        $this->dao = new UsersDao();
    }
    /**
     * @desc: create one Users    
     * @param varchar   $email     
     * @param varchar   $password                
     * @author: generate
     */
    public function registerOneUsers(
        $email,
        $password,
    ) {
        // check email
        if(empty($email)) return ['code' => 0, 'msg' => '邮箱不能为空','data' => []];
        if(empty($password)) return ['code' => 0, 'msg' => '密码不能为空','data' => []];

        $where = ['email' => $email,'is_del' => 0];
        $find = $this->dao->search($where)->find();
        if(!empty($find)) return ['code' => 0, 'msg' => '邮箱已存在','data' => []];



        $password = sha1($password);
        // to do  some check
        $data = compact('password','email');

        $data['is_del'] = 0;
        $data['status'] = 1;
        $data['create_at'] =  date('Y-m-d H:i:s');

        $id = Db::transaction(function () use($data) {
            $id = $this->create($data);
            // todo other
            return $id;
        });
        return ['code' => 200, 'msg' => 'ok', 'data' => ['id' => $id]];
    }

    /**
     * @desc: update one Users
     * param int  id
     * @param varchar   $email         用户电子邮箱地址  
     * @param varchar   $password      密码哈希值
     * @author: generate
     */
    public function loginOneUsers(
        $email = null,
        $password = ''
    ) {

        if(empty($email)) return ['code'=>0,'msg'=>'邮箱不能为空'];
        if(empty($password)) return ['code'=>0,'msg'=>'密码不能为空'];

        $check = $this->dao->search(['email'=>$email,'is_del' =>0])->find();
        if(empty($check)) return ['code'=>0,'msg'=>'用户不存在'];

        $password = sha1($password);
        $where = ['email'=>$email,'password'=>$password,'is_del' =>0];
        $user =  $this->dao->search($where)->find();
        if(empty($user)) return ['code'=>0,'msg'=>'密码错误'];


        $update = ['token' => md5($user->id.time()),'last_login_at' => date('Y-m-d H:i:s'),'last_login_ip' => request()->getRealIp()];
        
        $res = $this->update($user->id,$update);

        if($res){
            return ['code' => 200, 'msg' => 'ok', 'data' => $update];
        }else{
            return ['code' => 0, 'msg' => '登录失败，请重试', 'data' => []];
        }        
    }




    /**
     * @desc: getDetail
     * @author: generate
     */
    public function getDetail($id,$user_id = 0){
        if(empty($id)) return [];
        if(!$this->exists($id)) return [];
        $info = $this->dao->search(['id' => $id])->hidden(['password','token'])->find();
        if (empty($info)){
            return [];
        }
        $info = $this->formateItemRowDetail($info,$user_id);
        return $info;
    }

    /**
     * @desc: get one user by token
     * @author: xiaohp
     * @param {*} $token
     */
    public function getOneUserByToken($token){
        $user = $this->dao->search(['token' => $token,'status' => 1,'is_del' =>0])->find();
        return $user;
    }

    /**
     * @desc: get user by email
     * @author: xiaohp
     * @param {*} $email
     */
    public function getUserByEmail($email){
        $user = $this->dao->search(['email' => $email,'is_del' =>0])->find();
        return $user;
    }
    /**
     * @desc: user login out
     * @author: xiaohp
     * @param {*} $user_id
     */
    public function loginout($user_id){
        if(empty($user_id)) return ['code' => 0, 'msg' => '请选择操作的项', 'data' => []];
        if(!$this->exists($user_id)) return ['code' => 0, 'msg' => '操作的项不存在', 'data' => []];
        $update = ['token' => ''];
        $res = $this->update($user_id, $update);
        return ['code' => 200, 'msg' => 'ok', 'data' => []];
    }
  
}
